#均值滤波 高斯滤波 中值滤波
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img=cv.imread('F:\\11\\40.PNG',1)
img1=cv.imread('F:\\11\\20.png',1)
blur=cv.blur(img,(5,5))
def salt_paper_noise(img,prob):#添加椒盐噪声
    salt=np.zeros(img.shape,np.uint8)
    thres=1-prob
    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            rdn=np.random.rand()
            if rdn<prob:
                salt[i][j]=0
            elif rdn>thres:
                salt[i][j]=255
            else:
                salt[i][j]=img[i][j]
    return salt
def GSblur():#高斯滤波
    blur=cv.GaussianBlur(img,(3,3),1)
    return blur
def MEblur(image):#中值滤波
    blur=cv.medianBlur(image,5)
    return blur
def show(img,name):#利用matplotlib显示图像
    plt.imshow(img[:, :, ::-1])
    plt.title(name), plt.xticks([]), plt.yticks([])
    plt.show()
def show1(img,blur):
    plt.figure(figsize=(10,8),dpi=100)
    plt.subplot(121),plt.imshow(img[:,:,::-1]),plt.title('origin')
    plt.xticks([]),plt.yticks([])
    plt.subplot(122),plt.imshow(blur[:,:,::-1]),plt.title('blur')
    plt.xticks([]),plt.yticks([])
    plt.show()
if __name__=="__main__":
    show1(salt_paper_noise(img,0.05),MEblur(salt_paper_noise(img,0.05)))

